package cn.vic.db.sql;

import cn.vic.model.TableUpdateModel;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by vic on 2017/8/25.
 */
public class BaseSqlFormat implements BaseSqlInterface {
    /**
     * 查询单个字段，并出来集合
     *
     * @param connection
     * @param sql
     * @return
     * @throws SQLException
     */
    protected List<String> findDbList(Connection connection, String sql) throws SQLException {
        PreparedStatement pstmt = connection.prepareStatement(sql);
        ResultSet resultSet = pstmt.executeQuery();//返回查询结果
        List<String> list = new ArrayList<String>();

        ResultSetMetaData metaData = resultSet.getMetaData();
        int col_len = metaData.getColumnCount();
        while (resultSet.next()) {
            for (int i = 0; i < col_len; i++) {
                String cols_name = metaData.getColumnName(i + 1);
                Object cols_value = resultSet.getObject(cols_name);
                if (cols_value == null) {
                    cols_value = "";
                }
                list.add(cols_value.toString());
            }
        }
        resultSet.close();
        connection.isReadOnly();
        return list;
    }

    @Override
    public String getUpdateRemarksSql(TableUpdateModel updateModel) {
        return null;
    }
}
